package oocl.ita.eordering.webService;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import oocl.ita.eordering.po.Food;
import oocl.ita.eordering.service.FoodService;
@Component
public class GetFoodRecommendWS {
	@Autowired(required = true)
	private FoodService fService;
	private static Properties prop = new Properties();

	public FoodService getfService() {
		return fService;
	}

	public void setfService(FoodService fService) {
		this.fService = fService;
	}

	public List<String> getMechantPass() {// public void getMechantPass( MerchantService
									// mService) {
		Client client = Client.create();
		client.setReadTimeout(1000);
		try {
			prop.load(new FileInputStream(GetFoodRecommendWS.class.getClassLoader().getResource("/ws.properties").getPath()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = prop.getProperty("statUrlforFood");
		System.out.println("Food url："+url);
		WebResource wr = client.resource(url);
//		WebResource wr = client.resource("http://localhost:3000/food/get/passFId");
		String fPassIds = wr.accept(MediaType.APPLICATION_JSON_TYPE)// accept请求头
				.get(String.class);
		fPassIds = fPassIds.substring(0, fPassIds.length() - 1);
		String[] fpIds = fPassIds.split(",");
		List<String> fidList = new ArrayList<String>();
		for (String string : fpIds) {
			System.out.println(string);
			fidList.add(string);
		}
		//fidToFoods(fpIds);// midToMerchants(mpIds,mService);
		return fidList;

	}

	public List<Food> fidToFoods(String[] fpIds) {
		// public List<Merchant> midToMerchants(String[] mpIds, MerchantService
		// mService) {
		List<Food> foodsRecommend = new ArrayList<Food>();
		List<Food> foods = fService.findAllFood();
		for (Food food : foods) {
			System.out.println(" fService.findAllFood();"+food.getfName()+"...."+food.getfStatus());
		}
		for (Food food : foods) {
			for (int i = 0; i < fpIds.length - 1; i++) {
				if (fpIds[i].equals(food.getfId())) {
					foodsRecommend.add(food);
					break;
				}
			}
		}
		{
			for (Food food : foodsRecommend) {
				System.out.println(".....foodrecommand" + food.getfId());
			}
			return foodsRecommend;
		}
	}
}
